#!/bin/bash

# user permission check
if [ ! $(id -u) = 0 ]; then
  echo "Error: Tor2web install script must be runned by root"
  exit 1
fi

# Preliminary Requirements Check
ERR=0
echo "Checking preliminary Tor2web requirements"
for REQ in apt-key apt-get
do
  if which $REQ >/dev/null; then
    echo " + $REQ requirement meet"
  else
    ERR=$(($ERR+1))
    echo " - $REQ requirement not meet"
  fi
done

if [ $ERR -ne 0 ]; then
  echo "Error: Found ${ERR} unmet requirements"
  exit 1
fi

DO () {
  if [ -z "$2" ]; then
    EXPECTED_RET=0
  else
    EXPECTED_RET=$2
  fi
  if [ -z "$3" ]; then
    CMD=$1
  else
    CMD=$3
  fi
  echo -n "Running: \"$CMD\"... "
  eval $CMD &>${LOGFILE}

  STATUS=$?

  echo $CMD > $TMPDIR/last_command
  echo $STATUS > $TMPDIR/last_status

  if [ "$STATUS" -eq "$EXPECTED_RET" ]; then
    echo "SUCCESS"
  else
    echo "FAIL"
    echo "Ouch! The installation failed."
    echo "COMBINED STDOUT/STDERR OUTPUT OF FAILED COMMAND:"
    cat ${LOGFILE}
    exit 1
  fi
}

LOGFILE="./install.log"
ASSUMEYES=0

GLOBALEAKS_PGP_KEY="
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBFFtX2EBEADWMQ9CpB55LcQzg1JS2oCzOcHN3oWQwfluIJltFPzbUC8KSTJr
rSKghSIzgA9C5ltoFgqwhZCiwQX0sFHLHw0+WQLXDqyRcJWCmL1GVIvAN1xW5aPA
jvZ14TJJiajYF+q0v2Lm8JCtD4hk1QcpJE+IOiSMMDqu9nM9ic8+xJZKYYhlCUWv
AWKTORhRhYhImJkV5P6soozv/rHizXnQW4rzsTPSlMh8cptVx4PL9ShIrmNC9oyI
dBFLGskOk9IxE6vW16YocQgwkFkT4KGIhvq3fUyJSj+AmoxmThvY+9Y5eN8FQdFh
/hH/ndU8+I9U/tDKFdII+A6tl0sbrnFKw0AG++dZ7ZMeRFKFi76xyGAS1Juqbgat
c35U3V6UF4RAHAc1GYMs2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gNEM5+v8RehLC
5kXQWUc30gHRJFwjavzP49eKkh5burVuQWoR9g3rkWHCDKTZ76/RybLEihCSWlhY
uWINnV2zmop/UYkiw/UoG7Vh7+9blf1jMrj5OqETyZNdqSkyYODpKX7Tkiv1QTVp
TToHe4Peu9TsMemG8VZlpC/d1uQuWmsFBUNqZrKORcYrgJkdMqnXIrRzIZZfGGkb
8xnheFem0ZbRw6lAjfrMFF5Agm4zIqpAiHM7p7NU0w==
=VudL
-----END PGP PUBLIC KEY BLOCK-----"

for arg in "$@"; do
  shift
  case "$arg" in
    --assume-yes ) ASSUMEYES=1; shift;;
    -- ) shift; break;;
    * ) break;;
  esac
done

DISTRO="unknown"
DISTRO_CODENAME="unknown"
if which lsb_release >/dev/null; then
  DISTRO="$(lsb_release -is)"
  DISTRO_CODENAME="$(lsb_release -cs)"
  REAL_DISTRO=$DISTRO
  REAL_DISTRO_CODENAME=$DISTRO_CODENAME
fi

# LinuxMint is based on Ubuntu, if we encounter Mint just allign the Ubuntu version is based upon
if [ "$DISTRO" == "LinuxMint" ]; then
  DISTRO="Ubuntu"
  DISTRO_CODENAME=$(grep UBUNTU_CODENAME /etc/os-release | sed -e 's/UBUNTU_CODENAME=//')
fi

# The supported platforms are experimentally more than only Ubuntu as
# publicly communicated to users.
#
# Depending on the intention of the user to proceed anyhow installing on
# a not supported distro we using the experimental package if it exists
# or buster as fallback.
if echo "$DISTRO_CODENAME" | grep -vqE "^(buster|bionic)"; then
  # In case of unsupported platforms we fallback on Buster
  echo "Detected OS: $DISTRO - $DISTRO_CODENAME, which is not supported: fallback to 'buster'"
  DISTRO="Debian"
  DISTRO_CODENAME="buster"
else
    echo "Detected OS: $DISTRO - $DISTRO_CODENAME"
fi

echo "Adding GlobaLeaks PGP key to trusted APT keys"
TMPFILE=$TMPDIR/globaleaks_key
echo "$GLOBALEAKS_PGP_KEY" > $TMPFILE
DO "apt-key add $TMPFILE"
DO "rm -f $TMPFILE"

DO "apt-get update -y"

echo "Installing software-properties-common"
DO "apt-get install software-properties-common -y"

# try adding universe repo only on Ubuntu
if echo "$DISTRO" | grep -qE "^(Ubuntu)$"; then
  if ! grep -q "^deb .*universe" /etc/apt/sources.list /etc/apt/sources.list.d/*; then
    echo "Adding Ubuntu Universe repository"
    DO "add-apt-repository 'deb http://archive.ubuntu.com/ubuntu $DISTRO_CODENAME universe'"
  fi
fi

if [ ! -f /etc/apt/sources.list.d/globaleaks.list ]; then
  # we avoid using apt-add-repository as we prefer using /etc/apt/sources.list.d/globaleaks.list
  echo "deb http://deb.globaleaks.org $DISTRO_CODENAME/" > /etc/apt/sources.list.d/globaleaks.list
fi

DO "apt-get update -y"
DO "apt-get install tor2web -y"
